package com.stone.framework.message;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import io.swagger.annotations.ApiModelProperty;

public abstract class SortableBody {

    private String orderBy;

    /**
     * 默认将驼峰转下划线，适用mybatis-plus字段排序
     * 
     * @return
     */
    @ApiModelProperty(hidden = true)
    @JsonIgnoreProperties
    public String getUnderScoreCaseOrderBy() {
        return convert(this.orderBy);
    }

    public String getOrderBy() {
        return this.orderBy;
    }

    public void setOrderBy(String orderBy) {
        this.orderBy = orderBy;
    }

    /*
     * 驼峰转下划线
     */
    private String convert(String field) {
        Matcher matcher = Pattern.compile("[A-Z]").matcher(field);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

}
